Provide xvasprintf and implement xasprintf in terms of that.
authorrobertl <robertl>
Fri, 6 Oct 2006 16:30:25 +0000 (16:30 +0000)
committerrobertl <robertl>
Fri, 6 Oct 2006 16:30:25 +0000 (16:30 +0000)
util.c

diff --git a/util.c b/util.c
index 7dfcb446d10a19d45bb17461c492ad4c3b0983cd..f05f7d120cfa1a21db02b4fcb684caf448fb4a88 100644 (file)
--- a/util.c
+++ b/util.c
@@ -291,16 +291,15 @@ xfputs(const char *errtxt, const char *s, FILE *stream)
  */
 
 int
-xasprintf(char **strp, const char *fmt, ...)
+xvasprintf(char **strp, const char *fmt, va_list args)
 {
        /* From http://perfec.to/vsnprintf/pasprintf.c */
 /* size of first buffer malloc; start small to exercise grow routines */
 #define        FIRSTSIZE       64
-       va_list args;
-       char *buf;
+       char *buf = NULL;
        size_t bufsize;
        char *newbuf;
-       size_t nextsize;
+       size_t nextsize = 0;
        int outsize;
 
        bufsize = 0;
@@ -320,9 +319,7 @@ xasprintf(char **strp, const char *fmt, ...)
                        return -1;
                }
 
-               va_start(args, fmt);
                outsize = vsnprintf(buf, bufsize, fmt, args);
-               va_end(args);
 
                if (outsize == -1) {
                        /* Clear indication that output was truncated, but no
@@ -364,6 +361,20 @@ xasprintf(char **strp, const char *fmt, ...)
        return 0;
 }
 
+int
+xasprintf(char **strp, const char *fmt, ...)
+{
+       va_list args;
+       int rval;
+
+       va_start(args, fmt);
+       rval = xvasprintf(strp, fmt, args);
+       va_end(args);
+
+       return rval;
+       
+}
+
 /* 
  * Duplicate a pascal string into a normal C string.
  */